package com.macro.mall.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.api.ResultCode;
import com.macro.mall.common.exception.Asserts;
import com.macro.mall.common.utils.Assist;
import com.macro.mall.mapper.CmsMemberReportMapper;
import com.macro.mall.model.CmsMemberReport;
import com.macro.mall.model.UmsMember;
import com.macro.mall.service.CmsMemberReportService;
import com.macro.mall.service.UmsMemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
/**
 * CmsMemberReport的服务接口的实现类
 * 
 * @author gaodx
 *
 */
@Slf4j
@Service
public class CmsMemberReportServiceImpl implements CmsMemberReportService {

    private boolean isDebugEnabled;

	@Autowired
	private CmsMemberReportMapper cmsMemberReportMapper;
	@Autowired
	private UmsMemberService umsMemberService;
	// TODO 当你看到这个方法时你应该创建一个工具类做通用的方法,定义自己的返回格式化

	@Override
	public List<CmsMemberReport> find(CmsMemberReport value) {
		//TODO这里可以做通过Assist做添加查询
        List<CmsMemberReport> result = null;
        try {
            result = cmsMemberReportMapper.selectCmsMemberReport(null);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行获取CmsMemberReport数据集-->结果:", result);
        }
		return result;
	}

    @Override
    public List<CmsMemberReport> find(String reportType, String reportMemberName, String name, String reportStatus, String handleStatus, Integer pageNum, Integer pageSize) {
        //TODO这里可以做通过Assist做添加查询
        List<CmsMemberReport> result = null;
        PageHelper.startPage(pageNum, pageSize);
        Assist assist = new Assist();
        try {
            if(StrUtil.isNotEmpty(reportType)) {
                assist.andEq("report_Type",reportType);
            }
            if(StrUtil.isNotEmpty(reportMemberName)) {
                List<UmsMember> umsMembers = umsMemberService.selectAllByUmsMember(UmsMember.builder().username(reportMemberName).build());
                if(umsMembers.size() >0){
                    assist.andEq("report_member_name",umsMembers.get(0).getId());
                }
            } else if (StrUtil.isNotEmpty(name)) {
                List<UmsMember> umsMembers = umsMemberService.selectAllByUmsMember(UmsMember.builder().name(name).build());
                if(umsMembers.size() >0){
                    assist.andLike("name","%" +umsMembers.get(0).getName() +"%");
                }
            }

            if(StrUtil.isNotEmpty(reportStatus)) {
                assist.andEq("report_status",reportStatus);
            }
            if(StrUtil.isNotEmpty(handleStatus)) {
                assist.andEq("handle_status",handleStatus);
            }
            assist.setOrder(Assist.order("cm.create_time",false));
            result = cmsMemberReportMapper.selectCmsMemberReport(assist);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行获取CmsMemberReport数据集-->结果:", result);
        }
        return result;
    }

    @Override
	public CmsMemberReport findOne(Long id) {
		if (id == null) {
			if (isDebugEnabled) {
				log.debug("执行通过CmsMemberReport的id获得CmsMemberReport对象-->失败:id不能为空");
			}
            Asserts.fail("执行通过CmsMemberReport的id获得CmsMemberReport对象-->失败:id不能为空");
		}
		CmsMemberReport result = null;
        try {
            result = cmsMemberReportMapper.selectCmsMemberReportById(id);
        } catch (Exception e) {
        log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
		if (isDebugEnabled) {
			log.debug("执行通过CmsMemberReport的id获得CmsMemberReport对象-->结果:", result);
		}
		return result;
	}

	@Override
	public CommonResult saveNotNull(CmsMemberReport value) {
		if (value == null) {
			if (isDebugEnabled) {
				log.debug("执行将CmsMemberReport中属性值不为null的数据保存到数据库-->失败:对象不能为空");
			}
                Asserts.fail("失败:对象不能为空");
		}
		int result = 0;
        try {
            result = cmsMemberReportMapper.insertNotNullCmsMemberReport(value);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
		if (isDebugEnabled) {
			log.debug("执行将CmsMemberReport中属性值不为null的数据保存到数据库-->结果:", result);
		}
            return CommonResult.success(null);
	}
	@Override
	public CommonResult updateNotNullById(CmsMemberReport value) {
		if (value == null) {
			if (isDebugEnabled) {
				log.debug("执行通过CmsMemberReport的id更新CmsMemberReport中属性不为null的数据-->失败:对象为null");
			}
                Asserts.fail("执行通过CmsMemberReport的id更新CmsMemberReport中属性不为null的数据-->失败:对象为null");
		}
		int result = 0;
        try {
            result = cmsMemberReportMapper.updateNotNullCmsMemberReportById(value);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
		if (isDebugEnabled) {
			log.debug("执行通过CmsMemberReport的id更新CmsMemberReport中属性不为null的数据-->结果:", result);
		}
        return CommonResult.success(null);
	}

	@Override
	public CommonResult deleteById(Long id) {
		if (id == null) {
			if (isDebugEnabled) {
				log.debug("执行通过CmsMemberReport的id删除CmsMemberReport-->失败:id不能为空");
			}
                Asserts.fail("执行通过CmsMemberReport的id删除CmsMemberReport-->失败:id不能为空");
		}
		int result = 0;
        try {
            result = cmsMemberReportMapper.deleteCmsMemberReportById(id);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
		if (isDebugEnabled) {
			log.debug("执行通过CmsMemberReport的id删除CmsMemberReport-->结果:", result);
		}
        return CommonResult.success(null);
	}


}